using System;
using EntLibEC.Cmn;
using EntLibEC.Info;
using EntLibEC.Info.RMA;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace EntLibEC.Dac.RMA
{
	/// <summary>
	/// Summary description for RMARegisterDac.
	/// </summary>
	public class RMARegisterDac
	{
		

		public RMARegisterDac()
		{			
		}

		public int Insert(RMARegisterInfo oParam)
		{
			string sql = @"INSERT INTO Ent_RMA_Register
                            (
                            SysNo, ProductSysNo, RequestType, CustomerDesc, 
                            CheckTime, CheckDesc, NewProductStatus, RevertStatus, 
                            OutBoundStatus, ReturnStatus, ResponseDesc, ResponseTime, 
                            RefundStatus, NextHandler, Memo, Status , ProductNo , IsWithin7Days
                            )
                            VALUES (
                            @SysNo, @ProductSysNo, @RequestType, @CustomerDesc, 
                            @CheckTime, @CheckDesc, @NewProductStatus, @RevertStatus, 
                            @OutBoundStatus, @ReturnStatus, @ResponseDesc, @ResponseTime, 
                            @RefundStatus, @NextHandler, @Memo, @Status , @ProductNo , @IsWithin7Days
                            )";

			SqlCommand cmd = new SqlCommand(sql);

			SqlParameter paramSysNo = new SqlParameter("@SysNo", SqlDbType.Int,4);
			SqlParameter paramProductSysNo = new SqlParameter("@ProductSysNo", SqlDbType.Int,4);
			SqlParameter paramRequestType = new SqlParameter("@RequestType", SqlDbType.Int,4);
			SqlParameter paramCustomerDesc = new SqlParameter("@CustomerDesc", SqlDbType.NVarChar,500);
			SqlParameter paramCheckTime = new SqlParameter("@CheckTime", SqlDbType.DateTime);
			SqlParameter paramCheckDesc = new SqlParameter("@CheckDesc", SqlDbType.NVarChar,500);
			SqlParameter paramNewProductStatus = new SqlParameter("@NewProductStatus", SqlDbType.Int,4);
			SqlParameter paramRevertStatus = new SqlParameter("@RevertStatus", SqlDbType.Int,4);
			SqlParameter paramOutBoundStatus = new SqlParameter("@OutBoundStatus", SqlDbType.Int,4);
			SqlParameter paramReturnStatus = new SqlParameter("@ReturnStatus", SqlDbType.Int,4);
			SqlParameter paramResponseDesc = new SqlParameter("@ResponseDesc", SqlDbType.NVarChar,200);
			SqlParameter paramResponseTime = new SqlParameter("@ResponseTime", SqlDbType.DateTime);
			SqlParameter paramRefundStatus = new SqlParameter("@RefundStatus", SqlDbType.Int,4);
			SqlParameter paramNextHandler = new SqlParameter("@NextHandler", SqlDbType.Int,4);
			SqlParameter paramMemo = new SqlParameter("@Memo", SqlDbType.NVarChar,200);
			SqlParameter paramStatus = new SqlParameter("@Status", SqlDbType.Int,4);
			SqlParameter paramProductNo = new SqlParameter("@ProductNo", SqlDbType.NVarChar,50);
			SqlParameter paramIsWithin7Days = new SqlParameter("@IsWithin7Days", SqlDbType.Int,4);

			if ( oParam.SysNo != AppConst.IntNull)
				paramSysNo.Value = oParam.SysNo;
			else
				paramSysNo.Value = System.DBNull.Value;
			if ( oParam.ProductSysNo != AppConst.IntNull)
				paramProductSysNo.Value = oParam.ProductSysNo;
			else
				paramProductSysNo.Value = System.DBNull.Value;
			if ( oParam.RequestType != AppConst.IntNull)
				paramRequestType.Value = oParam.RequestType;
			else
				paramRequestType.Value = System.DBNull.Value;
			if ( oParam.CustomerDesc != AppConst.StringNull)
				paramCustomerDesc.Value = oParam.CustomerDesc;
			else
				paramCustomerDesc.Value = System.DBNull.Value;
			if ( oParam.CheckTime != AppConst.DateTimeNull)
				paramCheckTime.Value = oParam.CheckTime;
			else
				paramCheckTime.Value = System.DBNull.Value;
			if ( oParam.CheckDesc != AppConst.StringNull)
				paramCheckDesc.Value = oParam.CheckDesc;
			else
				paramCheckDesc.Value = System.DBNull.Value;
			if ( oParam.NewProductStatus != AppConst.IntNull)
				paramNewProductStatus.Value = oParam.NewProductStatus;
			else
				paramNewProductStatus.Value = 0;
			if ( oParam.RevertStatus != AppConst.IntNull)
				paramRevertStatus.Value = oParam.RevertStatus;
			else
				paramRevertStatus.Value = System.DBNull.Value;
			if ( oParam.OutBoundStatus != AppConst.IntNull)
				paramOutBoundStatus.Value = oParam.OutBoundStatus;
			else
				paramOutBoundStatus.Value = System.DBNull.Value;
			if ( oParam.ReturnStatus != AppConst.IntNull)
				paramReturnStatus.Value = oParam.ReturnStatus;
			else
				paramReturnStatus.Value = System.DBNull.Value;
			if ( oParam.ResponseDesc != AppConst.StringNull)
				paramResponseDesc.Value = oParam.ResponseDesc;
			else
				paramResponseDesc.Value = System.DBNull.Value;
			if ( oParam.ResponseTime != AppConst.DateTimeNull)
				paramResponseTime.Value = oParam.ResponseTime;
			else
				paramResponseTime.Value = System.DBNull.Value;
			if ( oParam.RefundStatus != AppConst.IntNull)
				paramRefundStatus.Value = oParam.RefundStatus;
			else
				paramRefundStatus.Value = System.DBNull.Value;
			if ( oParam.NextHandler != AppConst.IntNull)
				paramNextHandler.Value = oParam.NextHandler;
			else
				paramNextHandler.Value = System.DBNull.Value;
			if ( oParam.Memo != AppConst.StringNull)
				paramMemo.Value = oParam.Memo;
			else
				paramMemo.Value = System.DBNull.Value;
			if ( oParam.Status != AppConst.IntNull)
				paramStatus.Value = oParam.Status;
			else
				paramStatus.Value = System.DBNull.Value;
			if ( oParam.ProductNo != AppConst.StringNull)
				paramProductNo.Value = oParam.ProductNo;
			else
				paramProductNo.Value = System.DBNull.Value;

			if ( oParam.IsWithin7Days != AppConst.IntNull)
				paramIsWithin7Days.Value = oParam.IsWithin7Days;
			else
				paramIsWithin7Days.Value = System.DBNull.Value;


			cmd.Parameters.Add(paramSysNo);
			cmd.Parameters.Add(paramProductSysNo);
			cmd.Parameters.Add(paramRequestType);
			cmd.Parameters.Add(paramCustomerDesc);
			cmd.Parameters.Add(paramCheckTime);
			cmd.Parameters.Add(paramCheckDesc);
			cmd.Parameters.Add(paramNewProductStatus);
			cmd.Parameters.Add(paramRevertStatus);
			cmd.Parameters.Add(paramOutBoundStatus);
			cmd.Parameters.Add(paramReturnStatus);
			cmd.Parameters.Add(paramResponseDesc);
			cmd.Parameters.Add(paramResponseTime);
			cmd.Parameters.Add(paramRefundStatus);
			cmd.Parameters.Add(paramNextHandler);
			cmd.Parameters.Add(paramMemo);
			cmd.Parameters.Add(paramStatus);
			cmd.Parameters.Add(paramProductNo);
			cmd.Parameters.Add(paramIsWithin7Days);

			return SqlHelper.ExecuteNonQuery(cmd);
		}

		public int Update(Hashtable paramHash)
		{
			StringBuilder sb = new StringBuilder(200);
			sb.Append("UPDATE Ent_RMA_Register SET ");

			if ( paramHash != null && paramHash.Count != 0 )
			{
				int index = 0;
				foreach(string key in paramHash.Keys)
				{
					object item = paramHash[key];
					if ( key.ToLower()=="sysno" )
						continue;

					if ( index != 0 )
						sb.Append(",");
					index++;

					
					if (item is int)
					{
						if ( (int)item == AppConst.IntNull)
							sb.Append(key).Append("= null");
						else
							sb.Append(key).Append("=").Append(item.ToString());
					}
					else if ( item is decimal )
					{
						if ( (decimal)item == AppConst.DecimalNull )
							sb.Append(key).Append("= null");
						else
							sb.Append(key).Append("=").Append(item.ToString());
					}
					else if (item is string)
					{
						sb.Append(key).Append(" = ").Append(Util.ToSqlString(item.ToString()));
					}
					else if (item is DateTime)
					{
						sb.Append(key).Append(" = cast(").Append( Util.ToSqlString(((DateTime)item).ToString(AppConst.DateFormatLong))).Append(" as DateTime)");
					}
				}
			}

			sb.Append(" WHERE SysNo=").Append(paramHash["SysNo"]);

			return SqlHelper.ExecuteNonQuery(sb.ToString());
		}
	}
}
